package sk.stuba.fiit.repka.dp.owl.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;

import sk.stuba.fiit.repka.dp.owl.DataPropIndiAssociation;
import sk.stuba.fiit.repka.dp.owl.MyOWLDataProperty;

public class AddDataToIndividualDialog extends JDialog implements ActionListener{

	private final JPanel contentPanel = new JPanel();
	private JTextField valueTF;
	
	private List<MyOWLDataProperty> properties;
	
	private Map<JRadioButton, MyOWLDataProperty> mapper;
	private ButtonGroup group;
	
	private DataPropIndiAssociation output;
	
	public DataPropIndiAssociation getOutput() {
		return output;
	}

	/**
	 * Create the dialog.
	 */
	public AddDataToIndividualDialog(List<MyOWLDataProperty> properties) {
		this.properties = properties;
		this.mapper = new HashMap<JRadioButton, MyOWLDataProperty>();
		
		setTitle("Add Data Property To Individual");
		this.setModal(true);
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new GridLayout(1, 0, 0, 0));
			
		JScrollPane dataPropSP = new JScrollPane();
		dataPropSP.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		dataPropSP.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		contentPanel.add(dataPropSP);

		JPanel dataPropPanel = new JPanel();
		dataPropPanel.setLayout(new GridLayout(0, 1));
		dataPropSP.setViewportView(dataPropPanel);
		this.group = this.generateRadioButtons(dataPropPanel);
		
		{
			JPanel valuePanel = new JPanel();
			contentPanel.add(valuePanel);
			GridBagLayout gbl_valuePanel = new GridBagLayout();
			gbl_valuePanel.columnWidths = new int[]{0, 0};
			gbl_valuePanel.rowHeights = new int[]{0, 0, 0};
			gbl_valuePanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
			gbl_valuePanel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
			valuePanel.setLayout(gbl_valuePanel);
			{
				JLabel lblInsertValue = new JLabel("Insert Value");
				GridBagConstraints gbc_lblInsertValue = new GridBagConstraints();
				gbc_lblInsertValue.insets = new Insets(80, 0, 5, 0);
				gbc_lblInsertValue.gridx = 0;
				gbc_lblInsertValue.gridy = 0;
				valuePanel.add(lblInsertValue, gbc_lblInsertValue);
			}
			{
				valueTF = new JTextField();
				GridBagConstraints gbc_valueTF = new GridBagConstraints();
				gbc_valueTF.gridx = 0;
				gbc_valueTF.gridy = 1;
				valuePanel.add(valueTF, gbc_valueTF);
				valueTF.setColumns(15);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(this);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(this);
				buttonPane.add(cancelButton);
			}
		}
		
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}
	
	private ButtonGroup generateRadioButtons(JPanel panel) {
		ButtonGroup group = new ButtonGroup();
		Iterator<MyOWLDataProperty> iter = this.properties.iterator();
		while(iter.hasNext()) {
			MyOWLDataProperty prop = iter.next();
			JRadioButton bt = new JRadioButton(prop.getName());
			this.mapper.put(bt, prop); // map button to data property
			panel.add(bt);
			group.add(bt);
		}
		return group;
	}
	
	//map data property to its value
	private void mapDataToValue(JRadioButton selectedDataPropBT, String value) {
		MyOWLDataProperty prop = this.mapper.remove(selectedDataPropBT);
		this.output = new DataPropIndiAssociation(prop, value);
	}
	
	// return selected radio button from group of radio buttons
	private JRadioButton getSelection(ButtonGroup group) {
		for (Enumeration e = group.getElements(); e.hasMoreElements();) {
			JRadioButton b = (JRadioButton) e.nextElement();
			if (b.getModel() == group.getSelection()) {
				return b;
			}
		}
		return null;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if("OK".equals(e.getActionCommand())) {
			Toolkit toolkit = Toolkit.getDefaultToolkit();
			JRadioButton selectedDataBT = getSelection(this.group);
			String value = this.valueTF.getText();
			if(selectedDataBT != null && value != null && !"".equals(value)) {
				this.mapDataToValue(selectedDataBT, value);
				this.dispose();
			} else {
				toolkit.beep();
			}
				
		} else if("Cancel".equals(e.getActionCommand())) {
			this.dispose();
		}
	}
}
